#include "widget.h"
#include "ui_widget.h"
#include <QPushButton>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::keyPressEvent(QKeyEvent *event)
{
    if (event->modifiers() == Qt::ControlModifier)
    {
        if (event->key() == Qt::Key_A)
            qDebug() << "ctrl + A按键被按下";
    }

}
static QChar ch('a');
void Widget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        qDebug() << "press mouse left";
        QPushButton *btn = new QPushButton();
        QString ch1 = "hello" + QString(ch);
        ch = 'b';
        btn->setText(ch1);
        btn->setFixedSize(100, 200);
        btn->show();
    }
}

void Widget::on_pushButton_clicked()
{
    this->progDilo = new QProgressDialog(tr("正在复制"), tr("取消复制"), 0, 5000, this);
    this->progDilo->setWindowTitle(tr("文件复制对话框"));
    this->progDilo->setWindowModality(Qt::ApplicationModal);
    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &Widget::UpdateTimer);
    connect(progDilo, &QProgressDialog::canceled, this, &Widget::CancelTimer);
    timer->start(2);
    qDebug() << "复制结束";
}

void Widget::CancelTimer()
{

}

void Widget::UpdateTimer()
{
    this->counter++;
    if (counter > 5000)
    {
        timer->stop();

        delete timer;
        timer = nullptr;

        delete progDilo;
        progDilo = nullptr;

        counter = 0;
        return;
    }
    progDilo->setValue(counter);
}

